/*
Copyright (c) 2008,2009, David Beck

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
   @file t__demo1_client.cc
   @brief verify the server side of rt0d4/Rx security model

   @file t__demo1.cs.c
   @brief Automatically generated by rxgen

   @file t__demo1.h
   @brief Automatically generated by rxgen
*/

extern "C" {
  #include "t__demo1.h"
}
#include "rt0d4_rx.h"
#include "zfile.hh"

#ifndef WIN32
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <unistd.h>
#else
# include <windows.h>
# include <winsock.h>
# define in_addr_t in_addr
#endif /*WIN32*/
#include <assert.h>

using rt0d4::common::zfile;

/** @brief Verifies rt0d4/Rx security model */
namespace test_rt0d4_rx {

/** @test Verify that the callback is called w/ non null params */
RT0D4_CDECL
int validate_peer_cert_cb(
        const unsigned char * cert, size_t len)
{
  assert( cert != NULL && len > 0 );
  return 0;
}

/** @test Test client to verify the rt0d4/Rx security model */
int test_client( int argc, char ** argv )
{
#ifdef WIN32
   unsigned long svr_addr = inet_addr("127.0.0.1");
#else /*WIN32*/
   in_addr_t svr_addr = inet_addr("127.0.0.1");
#endif
  unsigned short svr_port = T__DEMO1_SERVER_PORT;
  struct rx_connection * conn_p = 0;
  struct rx_securityClass * sec_p = 0;
  double res = 0.1;
  zfile zf;
  unsigned char cert[4000];
  const char * algs[] = { "c2tnb239v2", 0 };
  int err;

  if ( rx_Init( 0 ) )
    goto error;

  if ( !( sec_p = rt0d4_rx_NewClientSecurityObject() ) )
    goto error;

  zf.read_file("../../rt0d4_sec/test/client/client.crt");
  zf.get_data(cert);
  rt0d4_rx_SetOwnCredentials(sec_p,"Login","Pass");
  rt0d4_rx_SetOwnCert(sec_p,cert,zf.get_size());
  rt0d4_rx_AddAlgorithms(sec_p, algs);
  rt0d4_rx_SetEcKeyStrength(sec_p, 239, 239 );
  rt0d4_rx_SetPeerCertValidateCallBack(sec_p, validate_peer_cert_cb);

  if ( !( conn_p = rx_NewConnection(
                     svr_addr,           /* host */
                     htons( svr_port ),  /* port */
                     T__DEMO1_SERVICE_ID,
                     sec_p,              /* secobj */
                     0 ) ) )   goto error;

  err = t__demo1_Multiply(conn_p, 1.1, 2.2, &res );
  if( err != 0 ) { printf("Call1 result: %f  [%d]\n",res,err); }

  err = t__demo1_Multiply(conn_p, 1.1, 2.2, &res );
  if( err != 0 ) { printf("Call2 result: %f  [%d]\n",res,err); }

  assert( res == (1.1*2.2) );

  rx_Finalize();
  printf("OK: result: %f\n",res);
  return 0;

error:
  printf( "ERROR\n" );
  return 1;
}

} // end of test_rt0d4_rx

using namespace test_rt0d4_rx;

int main( int argc, char ** argv )
{
  return test_client(argc, argv);
}

/* EOF */
